package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Card;
import com.woniuxy.oldmanapi.entity.Card;
import com.woniuxy.oldmanapi.entity.OlderHeight;
import com.woniuxy.oldmanapi.entity.RechargeRecord;
import com.woniuxy.oldmanapi.service.CardService;
import com.woniuxy.oldmanapi.service.RecordService;
import com.woniuxy.oldmanapi.vo.CardVo;
import com.woniuxy.oldmanapi.vo.RecordVo;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

import java.util.Date;

@RestController
public class CardController {

    @Autowired
    CardService cardService;
    @Autowired
    RecordService recordService;

    @GetMapping("/card/list")
    public ResponseResult<PageInfo<CardVo>> list(
            @RequestParam(value = "now", defaultValue = "1") Integer now,
            @RequestParam(value = "size", defaultValue = "3") Integer size,
            int balanceStatus,
            int status,
            String name) {
        ResponseResult<PageInfo<CardVo>> result = new ResponseResult<>();
        try {
            PageInfo<CardVo> lists = cardService.findCardByPage(now, size, balanceStatus, status, name);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }

    @GetMapping("/card/huanka")
    public ResponseResult<Void> list(
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "newNumber") String newNumber) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            Integer i = cardService.findIdByNumber(newNumber);
            if (i == null) {
                Card card1 = new Card();
                Card card = cardService.selectCard(id);
                card1.setBalance(card.getBalance());
                card1.setOlderId(card.getOlderId());
                card1.setStatus(1);
                card1.setNumber(newNumber);
                card1.setOpenDate(new Date());
                cardService.zhuxiao(id);
                cardService.add(card1);
                result.setCode(200);
                result.setMsg("换卡成功!");
            }else {
                result.setCode(202);
                result.setMsg("卡号重复，换个卡号吧!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("换卡失败!");
        }
        return result;
    }

    @GetMapping("/card/one/{id}")
    public ResponseResult<CardVo> one(@PathVariable("id") int id) {
        ResponseResult<CardVo> result = new ResponseResult<>();
        try {
            CardVo cardVo = cardService.selectOne(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(cardVo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }

    @PutMapping("/card/guashi/{id}")
    public ResponseResult<Void> guashi(@PathVariable("id") int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            cardService.guashi(id);
            result.setCode(200);
            result.setMsg("挂失成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("挂失失败!");
        }
        return result;
    }

    @PutMapping("/card/dongjie/{id}")
    public ResponseResult<Void> dongjie(@PathVariable("id") int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            cardService.dongjie(id);
            result.setCode(200);
            result.setMsg("冻结成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("冻结失败!");
        }
        return result;
    }

    @PutMapping("/card/zhuxiao/{id}")
    public ResponseResult<Void> zhuxiao(@PathVariable("id") int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            cardService.zhuxiao(id);
            result.setCode(200);
            result.setMsg("注销成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("注销失败!");
        }
        return result;
    }

    @PutMapping("/card/jiegua/{id}")
    public ResponseResult<Void> jiegua(@PathVariable("id") int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            cardService.jiegua(id);
            result.setCode(200);
            result.setMsg("解除挂失成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("解除挂失失败!");
        }
        return result;
    }

    @PutMapping("/card/jiedong/{id}")
    public ResponseResult<Void> jiedong(@PathVariable("id") int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            cardService.jiegua(id);
            result.setCode(200);
            result.setMsg("解除冻结成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("解除冻结失败!");
        }
        return result;
    }

    @PostMapping("/card/add")
    public ResponseResult<Void> add(@RequestBody RecordVo recordVo) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            Integer i = cardService.findIdByNumber(recordVo.getNumber());
            if (i == null) {
                Card card = new Card();
                card.setBalance(new BigDecimal(recordVo.getNum()));
                card.setNumber(recordVo.getNumber());
                card.setStatus(1);
                card.setOpenDate(new Date());
                card.setOlderId(recordVo.getOlderId());
                cardService.add(card);
                int k = cardService.selectMaxId();
                RechargeRecord rechargeRecord = new RechargeRecord();
                rechargeRecord.setDate(new Date());
                rechargeRecord.setRemark(recordVo.getRemark());
                rechargeRecord.setCardId(k);
                rechargeRecord.setNum(recordVo.getNum());
                rechargeRecord.setPaymode(recordVo.getPaymode());
                recordService.recharge(rechargeRecord);
                result.setCode(200);
                result.setMsg("添加成功!");
            } else {
                result.setCode(202);
                result.setMsg("卡号重复，换个卡号吧!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败!");
        }
        return result;
    }

    @GetMapping("/card/selectCardById/{id}")
    public ResponseResult<List<Card>> list(@PathVariable(value = "id") int id) {
        ResponseResult<List<Card>> result = new ResponseResult<>();
        try {
            List<Card> lists = cardService.findCardById(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }
}
